<?php include "header.php";include "yhteys.php"; 
//odotetaan
//haetaan tiedostosta seuraava uurna
$kahva = fopen ("rengas.txt", "r");
$numero = fgets($kahva);
fclose($kahva);

if (isset($_POST['tallenna']))
{        
    // suojataan merkkijonot ennen kyselyn suorittamista
    $nimi = pg_escape_string($_POST['nimi']);
    echo "suoritetaan <br/>";
    $kysely = "INSERT INTO rengas VALUES ($numero,'$nimi')";
	$tulos = pg_query($kysely);
    if (!$tulos) {
    echo "Virhe kyselyssä.\n";
    exit;
    } else {
	    $numero++;
		$kahva = fopen ("rengas.txt", "w");
		fwrite($kahva, $numero);
		fclose($kahva);
	}
	         	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RENGAS</title>
</head>
<body>
  
<h2>Renkaan lisääminen</h2>
<form name="input" action="" method="post">
Nimi:&nbsp;&nbsp;&nbsp; <input type="text" name="nimi" />
<br/>
ID: <?php echo $numero?>
<br/>
<br/>
<input type="hidden" name="tallenna" value="jep" />
<input type="submit" value="Suorita kysely" />
<br/>
</form> 
<br/>
<h2>Renkaat:</h2>
<?php 
$tulos = pg_query("SELECT * FROM rengas");
if (!$tulos) {
  echo "Virhe kyselyssä.\n";
  exit;
}

while ($row = pg_fetch_row($tulos)) {
  echo "ID: $row[0]  Nimi: $row[1]";
  echo "<br />\n";
}
pg_close($yhteys);
?>


		       

</body>
</html>